/***********************
 Bluetooth test program
***********************/

int counter = 0;
int incomingByte;
int ms = 1000;

void setup() {
  Serial.begin(57600);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);   
}

void loop() {
  // see if there's incoming serial data:
  ms = serReadInt();
  Serial.println("ms: "+ms);
  
  
  Serial.println(counter);
  counter++;
  digitalWrite(12, HIGH);   // set the LED on
  digitalWrite(13, LOW);
  delay(ms);              // wait for a second
  
  digitalWrite(12, LOW);    // set the LED off
  digitalWrite(13, HIGH);
  delay(ms);              // wait for a second
}

int serReadInt()
{
 int i, serAva;                           // i is a counter, serAva hold number of serial available
 char inputBytes [7];                 // Array hold input bytes
 char * inputBytesPtr = &inputBytes[0];  // Pointer to the first element of the array
     
 if (Serial.available()>0)            // Check to see if there are any serial input
 {
   delay(5);                              // Delay for terminal to finish transmitted
                                              // 5mS work great for 9600 baud (increase this number for slower baud)
   serAva = Serial.available();  // Read number of input bytes
   for (i=0; i<serAva; i++)       // Load input bytes into array
     inputBytes[i] = Serial.read();
   inputBytes[i] =  '\0';             // Put NULL character at the end
   return atoi(inputBytesPtr);    // Call atoi function and return result
 }
 else
   return 1000;                           // Return -1 if there is no input
}
